local gouchong = fk.CreateSkill{
  name = "hanqing__gouchong",
}

Fk:loadTranslationTable{
  ["hanqing__gouchong"] = "媾宠",
  [":hanqing__gouchong"] = "当其他角色受到伤害后，你可以将你场上一张牌移入其区域；当你回复体力后，你可以将场上一张牌移入你的区域。"..
  "若与上次发动的时机不同，你摸两张牌；若与上次移动的区域不同，你回复1点体力；若均满足，你的体力上限+1。",

  ["#hanqing__gouchong-invoke"] = "媾宠：是否将你场上一张牌移至 %dest？",
  ["#hanqing__gouchong-choose"] = "媾宠：你可以将场上一张牌移至你的区域内",
}

gouchong:addEffect(fk.Damaged, {
  anim_type = "control",
  can_trigger = function (self, event, target, player, data)
    return target ~= player and player:hasSkill(gouchong.name) and not target.dead and
      table.find(player:getCardIds("ej"), function (id)
        return player:canMoveCardInBoardTo(target, id)
      end)
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    if room:askToSkillInvoke(player, {
      skill_name = gouchong.name,
      prompt = "#hanqing__gouchong-invoke::"..target.id,
    }) then
      event:setCostData(self, {tos = {target}})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local result = room:askToMoveCardInBoard(player, {
      target_one = target,
      target_two = player,
      skill_name = gouchong.name,
      move_from = player,
    })
    if result == nil or player.dead then return end
    local area = result.card.type == Card.TypeEquip and "e" or "j"
    local yes1, yes2
    if player:getMark(gouchong.name) ~= 0 then
      yes1 = player:getMark(gouchong.name)[1] ~= 1
      yes2 = player:getMark(gouchong.name)[2] ~= area
    end
    room:setPlayerMark(player, gouchong.name, {1, area})
    if yes1 then
      player:drawCards(2, gouchong.name)
      if player.dead then return end
    end
    if yes2 then
      room:recover{
        who = player,
        num = 1,
        recoverBy = player,
        skillName = gouchong.name,
      }
      if player.dead then return end
    end
    if yes1 and yes2 then
      room:changeMaxHp(player, 1)
    end
  end,
})

gouchong:addEffect(fk.HpRecover, {
  anim_type = "control",
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(gouchong.name) and
      table.find(player.room:getOtherPlayers(player, false), function (p)
        return table.find(p:getCardIds("ej"), function (id)
          return p:canMoveCardInBoardTo(player, id)
        end) ~= nil
      end)
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room:getOtherPlayers(player, false), function (p)
      return table.find(p:getCardIds("ej"), function (id)
        return p:canMoveCardInBoardTo(player, id)
      end) ~= nil
    end)
    local to = room:askToChoosePlayers(player, {
      min_num = 1,
      max_num = 1,
      targets = targets,
      skill_name = gouchong.name,
      prompt = "#hanqing__gouchong-choose",
      cancelable = true,
    })
    if #to > 0 then
      event:setCostData(self, {tos = to})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    local result = room:askToMoveCardInBoard(player, {
      target_one = to,
      target_two = player,
      skill_name = gouchong.name,
      move_from = to,
    })
    if result == nil or player.dead then return end
    local area = result.card.type == Card.TypeEquip and "e" or "j"
    local yes1, yes2
    if player:getMark(gouchong.name) ~= 0 then
      yes1 = player:getMark(gouchong.name)[1] ~= 2
      yes2 = player:getMark(gouchong.name)[2] ~= area
    end
    room:setPlayerMark(player, gouchong.name, {2, area})
    if yes1 then
      player:drawCards(2, gouchong.name)
      if player.dead then return end
    end
    if yes2 then
      room:recover{
        who = player,
        num = 1,
        recoverBy = player,
        skillName = gouchong.name,
      }
      if player.dead then return end
    end
    if yes1 and yes2 then
      room:changeMaxHp(player, 1)
    end
  end,
})

return gouchong
